﻿using System.Web.UI.WebControls;

namespace ETestIT.UI
{
    using System;
    using Domain.Entities;
    using Presentation;
    using Core.Storage;
    using Domain.Repositories;
    using StructureMap;

    public partial class JobTitles : PageBase<JobTitles>
    {
        private const string EditedjobTitleKey = "editedJobTitle";
        private ISessionStorage sessionStorage;
        private IJobTitleRepository jobTitleRepository;
        protected void Page_Load(object sender, EventArgs e)
        {
            jobTitleAddEdit.Update += JobTitleAddEditUpdate;
            jobTitleRepository = ObjectFactory.GetInstance<IJobTitleRepository>();
            sessionStorage = ObjectFactory.GetInstance<ISessionStorage>();

            if (!IsPostBack)
                GetAllJobTitles();
            
        }
        public void GetAllJobTitles()
        {
            gvJobTitles.DataSource = jobTitleRepository.GetAll();
            gvJobTitles.DataBind();
        }

        protected void LnkEditCommand(object sender, CommandEventArgs e)
        {
            var jobTitleId = new Guid(e.CommandArgument.ToString());

            switch (e.CommandName)
            {
                case "JobTitleEdit":
                    {
                        jobTitleAddEdit.SetModel(jobTitleRepository.GetById(jobTitleId));
                        jobTitleAddEdit.Visible = Visible;
                        sessionStorage.Set(EditedjobTitleKey, jobTitleId);
                        GetAllJobTitles();
                        break;
                    }
                case "JobTitleDelete":
                    {
                        jobTitleRepository.Delete(jobTitleId);
                        GetAllJobTitles();
                        break;
                    }
               
                default:
                    jobTitleAddEdit.Visible = false;
                    break;
            }
        }

        protected void BtnCreateNewJobTitle_Clicked(object sender, EventArgs e)
        {
            jobTitleAddEdit.Visible = Visible;
        }

        public void JobTitleAddEditUpdate(object sender, EventArgs e)
        {
            JobTitle theJobTitle;
            Action<JobTitle> theActionOnJobTitle;

            if (sessionStorage.Contains(EditedjobTitleKey))
            {
                theJobTitle = jobTitleRepository.GetById(sessionStorage.Get<Guid>(EditedjobTitleKey));
                theActionOnJobTitle = u => { jobTitleRepository.Update(theJobTitle); };
            }
            else
            {
                theJobTitle = new JobTitle();
                theActionOnJobTitle = u => { jobTitleRepository.Add(theJobTitle); };
            }

            if (jobTitleAddEdit.IsInputValide())
            {
                jobTitleAddEdit.UpdateModelFromUserInput(theJobTitle);
                theActionOnJobTitle (theJobTitle);
                jobTitleAddEdit.Visible = false;
                GetAllJobTitles();

                if (sessionStorage.Contains(EditedjobTitleKey))
                    sessionStorage.Remove(EditedjobTitleKey);
            }

        }
    }
}